package com.zhuss.core.processor;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.area.CreateAreaBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.AreaDao;
import com.zhuss.core.db.entity.Area;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.OceanException;
import com.zhuss.util.JsonUtil;


/**
 * @description: 地域创建
 * @author: zhushanshuo
 * @create: Thu Apr 08 20:24:46 CST 2021
 **/
@Component("areaCreate")
public class AreaCreate extends AbstractProcessor<Object, CreateAreaBo> implements Processor<Object, CreateAreaBo> {

    @Autowired
    private AreaDao areaDao;
    
    @Autowired
	@Qualifier("areaRead")
	private AreaRead areaRead;
    
    
    /**
     * 校验
     */
    @Override
    public void check(CreateAreaBo bo) throws OceanException {
    	// TODO 创建 地域 校验， 请补充
    	String name = bo.getName();
    	if (name == null || "".equals(name.trim())) {
    		// throw new OceanException(ExceptionCode.地域name为空);
    	}
    	
    	List<Area> list = areaRead.list(name);
    	if (!list.isEmpty()) {
    		// throw new OceanException(ExceptionCode.地域name已存在);
    	}
    }
    
    /**
     * 操作方法
     */
    @Override
    public Object process(CreateAreaBo param) {
    	Area record = new Area();
    	record.setName(param.getName());
    	record.setParent(param.getParent());
    	record.setSort(param.getSort());

    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	areaDao.create(record);
    	param.setTargetId(record.getAreaId());
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(CreateAreaBo param) throws OceanException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.地域创建.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
